package com.project.nimsen.web.pub.tag;

import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.util.CollectionUtils;
import org.springframework.stereotype.Component;

import com.project.nimsen.dto.ArchivesDto;
import com.project.nimsen.entity.Coltype;
import com.project.nimsen.service.IArchivesService;
import com.project.nimsen.service.IColtypeService;
import com.project.nimsen.util.WebUtil;
import com.project.jdbc.utils.DateUtils;
import com.project.jdbc.utils.Page;

import freemarker.core.Environment;
import freemarker.template.TemplateDirectiveBody;
import freemarker.template.TemplateException;
import freemarker.template.TemplateModel;
/**
 * 用于得到一系列文档的信息
 * */
@Component("nimsen_documents")
public class DocumentsTag extends BaseTag {
	@Resource
	IColtypeService coltypeService;
	@Resource
	IArchivesService archivesService;
	/**
	 * @param number 栏目编号，如果是PARENT则表示为当前栏目的父栏目的文档
	 * 					OWNER则表示为当前栏目的文档，其他为指定栏
	 * 					目的文档，必填项
	 * @param num 数值类型，要显示文档的条数，如果pagesize也存在值，则不进行分页
	 * @param orderfile CREATE为创建时间排序，UPDATE为更改时间排序，默认为CREATE排序
	 * @param order ASC为升序，DESC为降序，默认为ASC
	 * @param pagesize 数值类型，每页显示的文档条数，如果大于0则表进行分
	 * 					页，如果num也有值则pagesize不起作用
	 * @param htmlcontent true与false 如果为true则包含html的内容，
	 * 					默认为false
	 * @param textcontent true与false 如果为true则包含文本的内容，默认为
	 * 					false
	 * 
	 * @return 返回的集合名称为documents
	 * */
	public void exec(Environment env, Map params, TemplateModel[] loopVars,
			TemplateDirectiveBody body) throws TemplateException, IOException {
		String number = getParam("number");
		//必选项验证
		if(StringUtils.isBlank(number))
			return;
		String colid = getAttr("coltypeid");
		try {
			//siteid
			String siteid = getSessionAttr("pub_siteid");
			ArchivesDto dto = new ArchivesDto();
			dto.setSiteid(siteid);
			if("PARENT".equals(number)||"OWNER".equals(number)){
				if(StringUtils.isBlank(colid))
					return;
				Coltype curcol = coltypeService.findEntityById(colid);
				//当前级别
				dto.setColnumber(curcol.getNumber());
				//如果查询的是父级信息
				if("PARENT".equals(number))
					dto.setColnumber(curcol.getPnumber());
			}else{
				dto.setColnumber(number);
			}
			Coltype fincol = coltypeService.findEntityByNumer(dto.getColnumber(), siteid);
			//查找的栏目文章列表不存在的处理情况
			if(fincol==null)
				return;
			//查询条件设置
			dto.setNum(getIntegerParam("num"));
			dto.setOrderfile(getParam("orderfile"));
			String order=getParam("order");
			if("DESC".equals(order))
				dto.setOrder("DESC");
			else
				dto.setOrder("ASC");
			dto.setPagesize(getIntegerParam("pagesize"));
			dto.setHtmlcontent(getParam("htmlcontent"));
			dto.setTextcontent(getParam("textcontent"));
			dto.setPageIndex(getIntegerAttr("pageIndex"));
			//查询所需要的信息
			List<Map<String, Object>> list = archivesService.findTagByDto(dto);
			if(CollectionUtils.isEmpty(list))
				return;
			//栏目访问链接
			String colurl = WebUtil.webUrl(request)+"/pub/col"+fincol.getVurl();
			//处理文章的访问链接
			for(Map<String, Object> map : list){
				String idObj = (String)map.get("id");
				map.put("vurl", colurl+"/"+idObj+".html");
//				Date createTime = (Date) map.get("createTime");
//				map.put("createTime", DateUtils.convertDate2String(createTime));
				Date updateTime = (Date) map.get("updateTime");
				map.put("updateTime", DateUtils.convertDate2String(updateTime));
			}
			outVariable("documents", list);
			if(dto.getPage()==null||StringUtils.isBlank(colid))
				return;
			//处理分页
			Page page = dto.getPage();
			//首页与上一页url
			Integer prive = page.getPageIndex()-1;
			outVariable("documents_pageCount", page.getPageCount());
			outVariable("documents_pageIndex", page.getPageIndex());
			if(prive<1){
			}else if(prive<2){
				outVariable("documents_firstPage", colurl+"?pageIndex=1");
			}else{
				outVariable("documents_firstPage", colurl+"?pageIndex=1");
				outVariable("documents_privePage", colurl+"?pageIndex="+prive);
			}
			//未页与下一页url
			Integer next = page.getPageIndex()+1;
			if(next<page.getPageCount()){
				outVariable("documents_nextPage", colurl+"?pageIndex="+next);
				outVariable("documents_lastPage", colurl+"?pageIndex="+page.getPageCount());
			}else if ((next-page.getPageCount())==0)
				outVariable("documents_lastPage", colurl+"?pageIndex="+page.getPageCount());
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
